define(["jquery","servers"], function ($,servers) {
    return Backbone.Model.extend({
        defaults: function () {
            return {
                defOperateStatus:[
                    {id: "1", name: "启用", showIds: ["5"]},
                    {id: "5", name: "暂停", showIds: ["1"]}
                ]
            };
        },
        getPageList:function(param){
            var self = this;
            var paramMap=servers.getPageParamMap();
            paramMap= $.ExtCommon.initMergeJson(paramMap,{promotionPlanId:"spread",startDate:"startTime",endDate:"endTime"});
            var newParam= $.ExtCommon.getMappingJson(param,paramMap);
            servers.strategy.pageList(param.campaignId,newParam,function(res){
                var newJson=[];
                if(res.success){
                    newJson=$.map(res.data.list,function (info) {
                        var newInfo= $.ExtCommon.getMappingJson(info,{
                            id:"id",name:"name",status:"status",statusName:"statusDesc",errMsg:"rejectMessage",unitPrice:"unitPrice",kpiName:"feeTypeDesc",
                            pv:"pv",click:"click",cr:"cr",price:"cpc",cost:"cost"
                        },true);
                        $.ExtCommon.conversionEmptyZero(newInfo,["pv","click","cost","cr","price"]);
                        self._conversionStatus(newInfo);
                        return newInfo;
                    });
                }else{
                    res.data={};
                }
                res.dataOriginal=res.data;
                res.data.list=newJson;
                self.set({dataPageList: res});
                self.trigger("bind:PageList");
            });
        },
        _conversionStatus:function (newJson) {
            newJson.status = newJson.status.toString();
            newJson.isXEdit = newJson.status === "1" || newJson.status === "5";
            newJson.isFail = newJson.status === "2";
        },
        updateStatus: function (param) {
            var self = this;
            var newParam=$.ExtCommon.getMappingJson(param, {id:"id",status:"status"});
            servers.strategy.updateStatus(param.campaignId,newParam, function (res) {
                var newJson={};
                if(res.success){
                    newJson={status:param.status};
                    newJson.statusName=param.status==="1"?"有效":"暂停中";
                    self._conversionStatus(newJson);
                }
                res.data=newJson;
                self.set({dataUpdStatus: {req:param,res:res}});
                self.trigger("update:Status");
            });
        },
        updatePrice: function (param) {
            var self = this;
            var newParam=$.ExtCommon.getMappingJson(param, {id:"id",unitPrice:"unitPrice"});
            servers.strategy.updatePrice(param.campaignId,newParam, function (res) {
                self.set({dataUpdPrice: {req:param,res:res}});
                self.trigger("update:Price");
            });
        },
        getInfo: function (param) {
            var self = this;
            if (param.id) {
                servers.strategy.info(param, function (res) {
                    var newJson = $.ExtCommon.getMappingJson(res.success ? res.data : {}, {
                        id: "id",name: "name",spread: "promotionPlanId",description: "description",startTime:"startTime",endTime:"endTime",timeType:"type", unitPrice:"unitPrice",dayBudget:"dayBudget",
                        spaceInfo:"strategySpaceListVos",creativeInfo:"strategyCreativeListVos",targetPack:"targetPackageId",targetData:"targetPackageDetailVo.targetParam",
                        targetPackName:"targetPackageName"
                    }, true);
                    newJson.timeType=newJson.timeType.toString();
                    newJson.isLong=newJson.timeType==="1";
                    newJson.isRange=newJson.timeType==="2";
                    newJson.spaceInfo=$.ExtCommon.isCheckEmpty(newJson.spaceInfo)?{}:$.ExtCommon.getMappingJson(newJson.spaceInfo[0], {
                        id:"id",name:"name",adFormat:"adFormatDesc",describe:"description",media:"mediaName", platform:"osTypeDesc"
                    }, true);

                    newJson.creativeInfo=$.ExtCommon.isCheckEmpty(newJson.creativeInfo)?{}:$.ExtCommon.getMappingJson(newJson.creativeInfo[0], {id:"id",name:"name"}, true);

                    if($.ExtCommon.isCheckEmpty(newJson.targetPack)){
                        newJson.targetPack="-1";
                    }else{
                        newJson.targetPackName="";
                    }
                    newJson.isTargetPack=false;
                    newJson.targetData=$.ExtCommon.getMappingJson(newJson.targetData, {
                        time:"hours",area:"area",interest:"interests",gender:"gender",education:"education",marriage:"marriage",age:"age",
                        connection:"networkType",os:"os",telCom:"networkOperator",keyword:"keyword",appAction:"app",crowd:"customAudience"
                    });
                    if(!$.ExtCommon.isCheckEmpty(newJson.targetData.appAction)){
                        newJson.targetData.appAction=$.ExtCommon.getMappingJson(newJson.targetData.appAction,{category:"objects",action:"acts",fromDay:"timeWindow"})
                    }
                    if(!$.ExtCommon.isCheckEmpty(newJson.targetData.crowd)){newJson.targetData.crowd=newJson.targetData.crowd.list}

                    res.dataOriginal = res.data;
                    res.data = newJson;
                    self.set({dataInfo: {isAdd: false, res: res}});
                    self.trigger('bind:Info');
                });
            }else {
                var dataDef={spaceInfo:{},creativeInfo:{},targetData:{},isTargetPack:false,targetPack:"-1"};
                self.set({dataInfo:{isAdd:true,res:servers.getSendSuccess(dataDef)}});
                self.trigger('bind:Info');
            }
        },
        addInfo: function (json) {
            var self = this;
            var data=this._getMappingToServer(json);
            servers.strategy.add(json.campaignId,data, function (res) {
                self.set({dataSave: {isAdd: true, req: json, res: res}});
                self.trigger('save:Info');
            });
        },
        updateInfo: function (json) {
            var self = this;
            var data=this._getMappingToServer(json);
            data.id=json.id;
            servers.strategy.update(json.campaignId,data, function (res) {
                self.set({dataSave: {isAdd: false, req: json, res: res}});
                self.trigger('save:Info');
            });
        },
        _getMappingToServer:function(json){
            var newJson=$.ExtCommon.getMappingJson(json, {
                name: "name",promotionPlanId: "spread",description: "description",type:"timeType",startTime:"startTime",endTime:"endTime",
                spaceId:"space",creativeId:"creative",unitPrice:"unitPrice"
            });
            newJson.startTime+=" 00:00:00";
            newJson.endTime+=" 23:59:59";
            if(json.timeTypeInfo.isLong){newJson.endTime="9999-12-31 23:59:59"}

            if(json.spreadInfo.isWeChat){newJson.dayBudget=json.dayBudget}

            newJson.targetParam=$.ExtCommon.getMappingJson(json.targetData, {
                hours:"time",area:"area",interests:"interest",gender:"gender",education:"education",marriage:"marriage",age:"age",
                connection:"connection",os:"os",telcom:"telCom",keyword:"keyword",customAudience:"crowd"
            });
            if(!$.ExtCommon.isCheckEmpty(json.targetData.appAction)){
                newJson.targetParam.behavior=$.ExtCommon.getMappingJson(json.targetData.appAction, {objectIds:"category",actIds:"action",timeWindow:"fromDay"});
            }
            if(json.targetPack.toString()==="-1") {
                newJson.targetParam.type = json.isTargetPack ? 2 : 1;
                newJson.targetName = json.targetPackName;
            }else{
                newJson.targetPackageId=json.targetPack;
            }
            return newJson;
        }
    });
});